home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3944 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.8 KB  |  95 lines

  1. Path: news.th-darmstadt.de!news!enno
  2. From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Copy constructing an already default constructed object
  5. Date: 26 Jan 1996 21:10:35 GMT
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Distribution: world
  8. Message-ID: <ENNO.96Jan26221035@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  9. References: <4e906b$stk@elaine32.Stanford.EDU>
  10. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  11. In-reply-to: brien@leland.Stanford.EDU's message of 25 Jan 1996 14:28:27 -0800
  12.  
  13. In article <4e906b$stk@elaine32.Stanford.EDU> brien@leland.Stanford.EDU (brien oberstein) writes:
  14.  
  15.    I'd like to get some opinions on the best/cleanest way
  16.    to accomplish the following:
  17.  
  18.    I've got an object which has already been constructed
  19.    via its default constructor which just sets all pointers
  20.    to NULL.  Whats the best way to deep-copy into it?
  21.  
  22.  
  23.    class A {
  24.    public:
  25.      A();             // default ctor
  26.      A(const A&)      // copy ctor
  27.      A(const char *)  // convert ctor 
  28.    }; 
  29.  
  30.    somefunc()
  31.    {
  32.      ...
  33.      A a0;
  34.      ...
  35.      A a1("blah");
  36.  
  37.      //
  38.      // How do you deep copy a1 into a0 ???
  39.      //
  40.      a0 = a1;         // no good. shallow copy
  41.      a0 = A(a1);         // no good. dtor called for temp A object
  42.      a0 = *new A(a1);   // no good. creates memory for a A object
  43.  
  44.      //
  45.      // the follow works but is tedious
  46.      //
  47.      A empty;         // create an empty object
  48.      A tmp(a1);         // deep-copy a1 to temp
  49.      a0 = tmp;         // shallow-copy temp to a0
  50.      tmp = empty;         // clear out temp so internals are not destructed
  51.  
  52.      //
  53.      // so overload = to make deep copies
  54.      //
  55.      a0 = a1;
  56.  
  57.    }
  58.  
  59.    //
  60.    // deep-copy =
  61.    //
  62.    A& A::operator =(const A& other)
  63.    {
  64.      A empty;
  65.      A tmp(other);
  66.      memcpy(this, &tmp, sizeof(A));
  67.      memcpy(&tmp, &empty, sizeof(A));
  68.      return *this;
  69.    }
  70.  
  71.  
  72.    I'd like to know what people think of the solution I've reached.
  73.    I figure that this type of shit is common enough so there should 
  74.    be some widely accepted solution to this problem.  Or maybe its
  75.    not, but believe me that the situation does occur.
  76.  
  77. A class should define its copy-semantics, not the clients of that class.
  78. Often assignment can be expressed in terms of destruction and copy-
  79. construction, ie.
  80.  
  81.         Class& operator = (const Class& c) {
  82.            if (this!=&c) {
  83.               this->~Class();       // cleanup
  84.               new (this) Class(c);  // perform copy-construction
  85.            }
  86.            return *this;
  87.         }
  88.  
  89. In this case the constructor, destructor and copy-constructor are
  90. enough to determine the copy-semantics of the class. If a deep-copy
  91. is needed _you_ have to provide a suitable implementation for them.
  92. BTW performing a bitwise-copy using 'memcpy' is usually a bad idea.
  93.  
  94.         Enno
  95.